import service from './http';

export class ApiClient {
    constructor() {}

    get(url: string, body?: any, parmes?: any, routeParmes?: any) {
        return service.get(this.buildUrl(url, parmes, routeParmes), body);
    }

    post(url: string, body?: any, parmes?: any) {
        return service.post(this.buildUrl(url, parmes), body);
    }

    put(url: string, body?: any, parmes?: any) {
        return service.put(this.buildUrl(url, parmes), body);
    }

    delete(url: string, body?: any, parmes?: any, routeParmes?: any) {
        return service.delete(this.buildUrl(url, parmes, routeParmes), body);
    }

    private buildUrl(url: string, parmes?: any, routeParmes?: any) {
        let parmesStr = '';
        if (routeParmes) {
            const routerParmesArray = Object.values(routeParmes);

            for (const item of routerParmesArray) {
                parmesStr += `\\${item}`;
            }

            url = url + parmesStr;
        }

        parmesStr = '';
        if (parmes) {
            const parmesArray = Object.entries(parmes);

            for (const item of parmesArray) {
                parmesStr += `${item[0]}=${item[1]}&`;
            }

            parmesStr = parmesStr.slice(0, parmesStr.length - 1);
            url = `${url}?${parmesStr}`
        }

        return url;
    }
}